"""
Copyright (c) 2022 Guangdong University of Technology
PhotLab is licensed under [Open Source License].
You can use this software according to the terms and conditions of the [Open Source License].
You may obtain a copy of [Open Source License] at: [https://open.source.license/]

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.

See the [Open Source License] for more details.

Author: Meng Xiang, Junjiang Xiang
Created: 2023/8/19
Supported by: National Key Research and Development Program of China
"""
import numpy as np
import math


def laser_cw(N_symbol, center_frequency, reference_frequency, sample_rate, average_power = 1e-3, linewidth = 100e3):
    """连续激光器
           Args:
               N_symbol: 符号数
               sample_rate: 采样率
               averaage_power: 输出功率
               linewidth: 激光器线宽
               center_frequency: 中心频率
               reference_frequency: 参考频率

           Returns:
               laser_output：光载波信号
           """
    # N_symbol = len(signal[0])

    std_PhaseNoise = np.sqrt(2 * np.pi * linewidth / (sample_rate))
    PhaseNoise_temp = std_PhaseNoise * np.random.randn(N_symbol)
    PhaseNoise = np.zeros(N_symbol)
    PhaseNoise[0] = PhaseNoise_temp[0]

    for Ind in range(1, N_symbol):
        PhaseNoise[Ind] = PhaseNoise[Ind - 1] + PhaseNoise_temp[Ind]

    Phase_CarrierOffset = np.arange(1, N_symbol+1, 1) * 2 * math.pi * (center_frequency-reference_frequency) / sample_rate

    # 光载波产生
    laser_output = np.sqrt(10 ** (average_power / 10)) * np.exp(1j * PhaseNoise) * np.exp(1j * Phase_CarrierOffset)

    return laser_output